package testExcel;

import com.alibaba.fastjson.*;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import utils.fileConvert.ExcelUtil;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Chen Yuan
 * @create 2023-05-17  9:31
 */
public class readKibanaReport {

	public static void main(String[] args) {


		try {
			String path = "C:\\Users\\zovan\\Desktop\\神农 Java - cyTest.xls";
			File file = new File(path);
			FileInputStream fileInputStream = new FileInputStream(file);
			MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);

			List<JSONObject> jsonObjects = ExcelUtil.readExcByFile(multipartFile, JSONObject.class);
			String regex = "(\\s\\{).*(}\\s)";

			StringBuilder stringBuilder = new StringBuilder();
			int row = 0;
			String sql = null;

			for (JSONObject jsonObject : jsonObjects) {
				String string = readKibanaReport.parseString(String.valueOf(jsonObject), regex);
				if ("".equals(string)) {
					continue;
				}
				Object datas = JSONObject.parseObject(string).get("datas");

				// ----- 按照第三方统一支付出参日志生成
				//sql = getSQLA(datas);
				// -----
				sql = getSQLB(datas);

				if (sql == null || "".equals(sql)) continue;
				row++;
				stringBuilder.append(sql).append("\n\n");
			}

			System.out.println(row);
			System.out.println("============================================");
			System.out.println(stringBuilder.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String parseString(String text, String regex) {
		text.replace("出参:", "出参: ");
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(text);
		StringBuilder sb = new StringBuilder();
		while (matcher.find()) {
			sb.append(matcher.group());
		}
		return sb.toString().trim().replace("\\", "");
	}

	private static String getSQLA(Object datas) {
		JSONObject payObject = JSONObject.parseObject(JSON.toJSONString(datas));
		//System.out.println(payObject.get("hisId") + ", " + payObject.get("innerHisId") + ", " + payObject.get("thirdId"));

		if ("".equals(payObject.get("hisId")) ||
				"".equals(payObject.get("innerHisId")) ||
				"".equals(payObject.get("thirdId")) ||
				null == payObject.get("hisId") ||
				null == payObject.get("innerHisId") ||
				null == payObject.get("thirdId")) {
			return null;
		}

		return readKibanaReport.createSQLA(String.valueOf(payObject.get("hisId")), String.valueOf(payObject.get("innerHisId")), String.valueOf(payObject.get("thirdId")));
	}

	public static String createSQLA(String hisId, String innerHisId, String thirdId) {
		return ("UPDATE order_pay_style\n" +
				"SET trade_no = '" + innerHisId + "', third_order_number = '" + thirdId + "'\n" +
				"WHERE execute_order_master_id in (\n" +
				"\tSELECT execute_order_master_id\n" +
				"\tFROM (\n" +
				"\t\tSELECT DISTINCT execute_order_master_id\n" +
				"\t\tFROM order_pay_style\n" +
				"\t\tWHERE pay_order_number = '" + hisId + "'\n" +
				"\t) temp\n" +
				");");
	}

	private static String getSQLB(Object datas) {
		List<JSONObject> jsonObjects = JSONArray.parseArray(JSON.toJSONString(datas), JSONObject.class);
		StringBuilder stringBuilder = new StringBuilder();

		for (JSONObject payObject : jsonObjects) {
			String orderPayStyleStr = String.valueOf(payObject.get("orderPayStyles"));
			if (null == payObject.get("orderPayStyles") ||
					Objects.equals(orderPayStyleStr, "[]")) {
				return null;
			}
			List<JSONObject> array = JSONArray.parseArray(orderPayStyleStr, JSONObject.class);
			String sql;

			String orderPayStyleId = null, tradeNo = "", thirdOrderNumber = "";
			for (JSONObject orderPayStyle : array) {
				if ("".equals(orderPayStyle.get("orderPayStyleId")) ||
						null == orderPayStyle.get("orderPayStyleId")) {
					continue;
				}
				orderPayStyleId = String.valueOf(orderPayStyle.get("orderPayStyleId"));

				if (!"".equals(orderPayStyle.get("tradeNo")) &&
						null != orderPayStyle.get("tradeNo") &&
						"null" != orderPayStyle.get("tradeNo")) {
					tradeNo = String.valueOf(orderPayStyle.get("tradeNo"));
				}
				if (!"".equals(orderPayStyle.get("thirdOrderNumber")) &&
						null != orderPayStyle.get("thirdOrderNumber") &&
						"null" != orderPayStyle.get("thirdOrderNumber")) {
					thirdOrderNumber = String.valueOf(orderPayStyle.get("thirdOrderNumber"));
				}

				if ("".equals(thirdOrderNumber) && "".equals(tradeNo)) {
					continue;
				}

				sql = readKibanaReport.createSQLB(orderPayStyleId, tradeNo, thirdOrderNumber);
				stringBuilder.append(sql).append("\n\n");
			}
		}

		return stringBuilder.toString();
	}

	private static String createSQLB(String orderPayStyleId, String tradeNo, String thirdOrderNumber) {
		String sql = "UPDATE order_pay_style\n" +
				"SET ";

		boolean flag = false;
		if (!"".equals(tradeNo)) {
			sql += "trade_no = '" + tradeNo + "' ";
			flag = true;
		}

		if (!"".equals(thirdOrderNumber)) {
			if (flag) {
				sql += ", ";
			}
			sql += " third_order_number = '" + thirdOrderNumber + "'";
		}
		sql += "\n" +
				"WHERE order_pay_style_id = '" + orderPayStyleId + "';";
		return sql;
	}
}
